require 'fileutils'
require File.join(pwd, '../../../build/jake.rb')

def build_extension(name, arch)
  if (ENV['RHO_PLATFORM'] == 'osx') or (ENV['RHO_PLATFORM'] == 'win32') or (ENV['RHO_PLATFORM'] == 'linux')
    args = ['-o', 'Makefile', '-r', '-spec', $qmake_spec, "#{name}.pro", 'CONFIG-=debug', 'CONFIG+=release', $qmakevars]
    if (ENV['RHO_PLATFORM'] == 'osx')
        args = ['-o', 'Makefile', '-r', '-spec', $qmake_spec, "-early", "QMAKE_DEFAULT_LIBDIRS=$(xcrun -show-sdk-path)/usr/lib", "#{name}.pro", 'CONFIG-=debug', 'CONFIG+=release', $qmakevars]
    end
    puts Jake.run($qmake,args)
    fail "[qmake #{name}.pro]" unless $? == 0
    #puts Jake.run($make, ['clean'])
    fail "[make clean]" unless $? == 0
    if (ENV['RHO_PLATFORM'] == 'win32')
      puts Jake.run($make, ['release'])
    else
      puts Jake.run($make, ['all'])
    end
    fail "[make all]" unless $? == 0
    return
  end

  objects = []
  mkdir_p $tempdir unless File.exist? $tempdir

  Dir.glob("*.c").each do |f|
    objname = File.join( $tempdir, File.basename( f.gsub(/\.c$/, '.o') ) )
    objects << ('"'+objname+'"')

    args = []
    args << "-I."
    args << "-I\"#{$rootdir}/platform/shared/ruby/include\""
    args << "-I\"#{$rootdir}/platform/shared\""
    args << "-I\"#{$rootdir}/platform/shared/common\""

    if ENV['RHO_PLATFORM'] == 'android'
      args << "-I\"#{$rootdir}/platform/shared/ruby/android\""
      args << "-I\"#{$rootdir}/platform/shared/ruby/generated\""
      cc_compile f, $tempdir, args or exit 1

    elsif ENV['RHO_PLATFORM'] == 'iphone'
      args << "-I\"#{$rootdir}/platform/shared/ruby/iphone\""
      args << "-D_XOPEN_SOURCE"
      args << "-D_DARWIN_C_SOURCE"
      args << "-isysroot \"#{$sdkroot}\""
      args << "-fno-common"
      args << "-arch #{arch}"
      args << "-O2"
      args << "-o \"#{objname}\""
      args << "-c"
      args << f
      cmdline = $gccbin + ' ' + args.join(' ')
      puts cmdline
      puts `#{cmdline}`
      fail "[#{cmdline}]" unless $? == 0

    end
  end

  mkdir_p $targetdir unless File.exist? $targetdir

  if ENV['RHO_PLATFORM'] == 'android'
    cc_ar ('"'+File.join( $targetdir, 'lib' + name + '.a' )+'"'), Dir.glob($tempdir + "/**/*.o").collect{|x| '"'+x+'"'} or exit 1

  elsif ENV['RHO_PLATFORM'] == 'iphone'
    args = []
    args << 'rcs'
    args << File.join( $targetdir, 'lib' + name + '.a' )
    args += objects
    cmdline = $arbin + ' ' + args.join(' ')
    puts cmdline
    puts `#{cmdline}`
    fail "[#{cmdline}]" unless $? == 0

  elsif ENV['RHO_PLATFORM'] == 'wm'
    args = []
    args << "/M4"
    args << name + ".sln"
    args << "\"Release|#{$sdk}\""
    cmdline = "\"" + $vcbuild + "\"" + ' ' + args.join(' ')
    puts cmdline
    puts `#{cmdline}`
    fail "[#{cmdline}]" unless $? == 0

    cp_r File.join($tempdir, "Release", name + ".lib"), $targetdir
  end
end

namespace "build" do
  task :config do
    $targetdir = ENV['TARGET_TEMP_DIR']
    raise "TARGET_TEMP_DIR is not set" if $targetdir.nil?
    $tempdir = ENV['TEMP_FILES_DIR']
    raise "TEMP_FILES_DIR is not set" if $tempdir.nil?
    $rootdir = ENV['RHO_ROOT']
    raise "RHO_ROOT is not set" if $rootdir.nil?
    $qmakevars = ENV['RHO_QMAKE_VARS']

    if ENV['RHO_PLATFORM'] == 'android'
      require File.join($rootdir, 'platform/android/build/androidcommon.rb')

      setup_ndk(ENV['ANDROID_NDK'],ENV['ANDROID_API_LEVEL'])

    elsif ENV['RHO_PLATFORM'] == 'iphone'
      $sdkroot = ENV['SDKROOT']
      raise "SDKROOT is not set" if $sdkroot.nil?
      $bindir = ENV['PLATFORM_DEVELOPER_BIN_DIR']
      raise "PLATFORM_DEVELOPER_BIN_DIR is not set" if $bindir.nil?
      $arch = ENV['ARCHS']
      raise "ARCHS is not set" if $arch.nil?
      $gccbin = $bindir + '/gcc'
      $arbin = $bindir + '/ar'

    elsif (ENV['RHO_PLATFORM'] == 'osx') or (ENV['RHO_PLATFORM'] == 'linux')
      $qmake = ENV['RHO_QMAKE']
      raise "RHO_QMAKE is not set" if $qmake.nil?
      $qmake_spec = ENV['RHO_QMAKE_SPEC']
      raise "RHO_QMAKE_SPEC is not set" if $qmake_spec.nil?
      $make = 'make'

    elsif ENV['RHO_PLATFORM'] == 'wm'
      $vcbuild = ENV['VCBUILD']
      raise "VCBUILD is not set" if $vcbuild.nil?
      $sdk = ENV['SDK']
      raise "SDK is not set" if $sdk.nil?

    elsif ENV['RHO_PLATFORM'] == 'win32'
      $vcbuild = ENV['VCBUILD']
      raise "VCBUILD is not set" if $vcbuild.nil?
      #$sdk = ENV['SDK']
      #raise "SDK is not set" if $sdk.nil?

      $qmake = ENV['RHO_QMAKE']
      raise "RHO_QMAKE is not set" if $qmake.nil?
      $qmake_spec = ENV['RHO_QMAKE_SPEC']
      raise "RHO_QMAKE_SPEC is not set" if $qmake_spec.nil?
      $make = 'nmake'

    end
  end

  task :all => :config do
    build_extension('bigdecimal', $arch)
  end
end

task :default => "build:all"
